package com.playball.lottery.customview;

import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Handler;
import android.util.AttributeSet;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.widget.Gallery;

import java.util.Timer;
import java.util.TimerTask;

/**
 * Created by Administrator on 2017/3/14.
 */

public class BarberGallery extends Gallery {



    public BarberGallery(Context paramContext) {
        super(paramContext);
    }

    public BarberGallery(Context paramContext, AttributeSet paramAttributeSet) {
        super(paramContext, paramAttributeSet);
        timer.schedule(task, 3000, 3000);
    }

    public BarberGallery(Context paramContext, AttributeSet paramAttributeSet, int paramInt) {
        super(paramContext, paramAttributeSet, paramInt);
    }

    private boolean isScrollingLeft(MotionEvent e1, MotionEvent e2) {
        return e2.getX() > e1.getX();
    }

    // 用户按下触摸屏、快速移动后松开
    public boolean onFling(MotionEvent paramMotionEvent1, MotionEvent paramMotionEvent2, float paramFloat1,
                           float paramFloat2) {
        int kEvent;
        if (isScrollingLeft(paramMotionEvent1, paramMotionEvent2)) {
            kEvent = KeyEvent.KEYCODE_DPAD_LEFT;
        } else {
            kEvent = KeyEvent.KEYCODE_DPAD_RIGHT;
        }
        onKeyDown(kEvent, null);
        return true;
    }

    private static final int timerAnimation = 1;
    @SuppressLint("HandlerLeak")
    private final Handler mHandler = new Handler() {
        public void handleMessage(android.os.Message msg) {
            switch (msg.what) {
                case timerAnimation:
                    int position = getSelectedItemPosition();
                    if (position >= 0) {
                        onKeyDown(KeyEvent.KEYCODE_DPAD_RIGHT, null);
                    }
                    break;
                default:
                    break;
            }
        };
    };
    private final Timer timer = new Timer();
    private final TimerTask task = new TimerTask() {
        public void run() {
            mHandler.sendEmptyMessage(timerAnimation);
        }
    };



}
